package cn.edu.zucc.online_market.ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import cn.edu.zucc.online_market.online_marketUtil;
import cn.edu.zucc.online_market.control.UserManager;
import cn.edu.zucc.online_market.model.BeanAddress;
import cn.edu.zucc.online_market.model.BeanUser;
import cn.edu.zucc.online_market.util.BaseException;;

public class FrmRegister extends JDialog implements ActionListener {
	private JLabel label_telephone = new JLabel(" 手   机   号      ");
	private JLabel label_pwd1 = new JLabel("  密       码      ");
	private JLabel label_pwd2 = new JLabel("再次输入密码  ");
	private JLabel label_name = new JLabel("姓名");
	private JLabel label_email = new JLabel("邮箱  ");
	private JLabel label_sex = new JLabel("性别");
	private JLabel label_curent_city = new JLabel("所在城市 ");
	private JLabel label_address = new JLabel("配送地址");
	private JLabel label_province = new JLabel("省");
	private JLabel label_city = new JLabel("市");
	private JLabel label_area = new JLabel("县/乡");
	private JLabel label_address_details = new JLabel("具体地址");
	
	
	private JTextField text_telephone = new JTextField(20);
	private JTextField text_user_name = new JTextField(20);
	private JTextField text_user_email = new JTextField(20);
	private JTextField text_current_city = new JTextField(20);
	private JTextField text_address_details = new JTextField(20);
	
	private JRadioButton radiobutton_user_sex_male = new JRadioButton("男",true);
	private JRadioButton radiobutton_user_sex_female = new JRadioButton("女");
	
	private JPasswordField text_pwd1 = new JPasswordField(20);
	private JPasswordField text_pwd2 = new JPasswordField(20);
	
	private JButton button_step1_exit = new JButton("退出");
	private JButton button_step2_exit = new JButton("退出");
	private JButton button_step3_exit = new JButton("退出");
	private JButton button_step1_next = new JButton("下一步");
	private JButton button_step2_next = new JButton("下一步");
	private JButton button_step2_preview = new JButton("上一步");
	private JButton button_step3_preview = new JButton("上一步");
	private JButton button_finish = new JButton("完成");
//	private JButton button_exit = new JButton("退出");
	
	private JComboBox combox_province ;
	private JComboBox combox_city  ;
	private JComboBox combox_area ;
	
//	private JButton  = new JButton("");
	private JPanel cards = new JPanel(new CardLayout());
	CardLayout cl;
	private JPanel work_pane_step1 = new JPanel();
	private JPanel work_pane_step2 = new JPanel();
	private JPanel work_pane_step3 = new JPanel();
	private JPanel step3_panel2 = new JPanel();
	
	private String str_province[] = {"浙江","广东","福建","江苏","上海"};
	private String str_city_zhejiang[] = {"杭州","宁波","温州","绍兴","湖州","嘉兴","金华","衢州","台州","丽水","舟山"};
	private String str_city_guangdong[] = {"广州","深圳","佛山","东莞","中山","珠海","江门","肇庆","粤东","汕头","潮州","揭阳","汕尾","湛江","茂名","阳江","云浮","韶关","清远","梅州","河源"};
	private String str_area_hangzhou[] = {"上城区","下城区","西湖区","江干区","拱墅区","滨江区","萧山区","余杭区","临安区","富阳区","建德","桐庐","淳安"};
	private String str_area_ningbo[] = {"海曙区","江东区","江北区","镇海区","北仑区","鄞州区","慈溪市","余姚市","奉化市","宁海县","象山县"	};
	private String str_area_guangzhou[] = {"越秀区","荔湾区","海珠区","天河区","白云区","黄埔区","番禺区","花都区","南沙区","增城区","从化区"};
	
	private String str_unfinished[] = {"   "};
	public FrmRegister(JDialog f,String s,boolean b) {
		super(f,s,b);
		
		this.create_work_pane_step1();
		this.create_work_pane_step2();
		this.create_work_pane_step3();
		
		cards.add(work_pane_step1,"card1");
		cards.add(work_pane_step2,"card2");
		cards.add(work_pane_step3,"card3");
		
		cl=(CardLayout)(cards.getLayout());
		cl.show(cards, "card1");
		this.getContentPane().add(cards);
		
		this.button_finish.addActionListener(this);
		this.button_step2_preview.addActionListener(this);
		this.button_step3_preview.addActionListener(this);
		button_step1_exit.addActionListener(this);
		button_step2_exit.addActionListener(this);
		button_step3_exit.addActionListener(this);
		button_step1_next.addActionListener(this);
		button_step2_next.addActionListener(this);
		this.combox_province.addActionListener(this);
		this.combox_city.addActionListener(this);
		
		this.setSize(400,300);
		this.setResizable(false);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==this.button_step1_exit || e.getSource()==this.button_step2_exit || e.getSource()==this.button_step3_exit) {
			this.dispose();
		}else if(e.getSource()==this.button_step1_next) {
			String user_telephone = this.text_telephone.getText();
			String pwd1 = new String(this.text_pwd1.getPassword());
			String pwd2 = new String(this.text_pwd2.getPassword());
			if(test_register_step1(user_telephone,pwd1,pwd2)) {
				if(!pwd1.equals(pwd2)) {	
				JOptionPane.showMessageDialog(null, "两次密码输入不一致","错误",JOptionPane.ERROR_MESSAGE);
				}else {
					this.cl.show(this.cards,"card2");
				}	
			}		
		}else if(e.getSource()==this.button_step2_next) {
			String user_name = this.text_user_name.getText();
			String user_sex = this.radiobutton_user_sex_female.getText();
			String user_email = this.text_user_email.getText();
			String user_current_city = this.text_current_city.getText();
			if(test_register_step2(user_name,user_sex,user_email,user_current_city)) {
				this.cl.show(this.cards,"card3");
			}		
		}else if(e.getSource()==this.button_step2_preview) {
			this.cl.show(this.cards,"card1");
		}else if(e.getSource()==this.button_step3_preview) {
			this.cl.show(this.cards,"card2");
		}else if(e.getSource()==this.button_finish) {
			//调用用户注册函数，实现注册账号功能，再调用地址注册函数，实现增加一个地址的功能。
			String user_name = this.text_user_name.getText();
			String user_pwd = new String(this.text_pwd1.getPassword());
			String user_sex = this.radiobutton_user_sex_male.isSelected()? "男":"女";
			String user_email = this.text_user_email.getText();
			String user_current_city = this.text_current_city.getText();		
			String address_details = this.text_address_details.getText();
			String user_telephone =  this.text_telephone.getText();
			String province = (String) this.combox_province.getSelectedItem();
			String city = (String) this.combox_city.getSelectedItem();
			String area = (String) this.combox_area.getSelectedItem();		
			if(test_register_step3(province,city,area,address_details)) {
				try {
					BeanUser user = online_marketUtil.userManager.register(user_name, user_pwd, user_sex, user_email, user_current_city, user_telephone);
					BeanAddress address = online_marketUtil.addressManager.add(user, user_name, user_telephone, province, city, area, address_details);
					this.setVisible(false);
					JOptionPane.showMessageDialog(null, "regist success！","congraduations",JOptionPane.ERROR_MESSAGE);
				} catch (BaseException e1) {
					try {
						online_marketUtil.userManager.delete(user_telephone);
					} catch (BaseException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					
					JOptionPane.showMessageDialog(null, e1.getMessage(),"错误",JOptionPane.ERROR_MESSAGE);
					return;
				}
				
			}
		}else if(e.getSource()==this.combox_city) {
			String city="";
			if(this.combox_city.getSelectedItem()!=null) {
				city = this.combox_city.getSelectedItem().toString();
			}
			String str_ptr1[] ;
			this.combox_area.removeAllItems();
			if(city.equals("杭州")) str_ptr1 = this.str_area_hangzhou;
			else if (city.equals("宁波")) str_ptr1 = this.str_area_ningbo;
			else if(city.equals("广州"))  str_ptr1 = this.str_area_guangzhou;
			else  str_ptr1 = this.str_unfinished;
			for(int i = 0; i<str_ptr1.length;i++) {
				this.combox_area.addItem(str_ptr1[i]);
			}
			this.step3_panel2.revalidate();
			this.step3_panel2.repaint();
		}else if(e.getSource()==this.combox_province) {
			String province = combox_province.getSelectedItem().toString();	
			String str_ptr2[],strptr3[];
			this.combox_city.removeAllItems();
			this.combox_area.removeAllItems();
			if(province.equals("浙江")) str_ptr2 = this.str_city_zhejiang;
			else if(province.equals("广东")) str_ptr2 = this.str_city_guangdong;
			else str_ptr2 = this.str_unfinished;
			for(int i=0;i<str_ptr2.length;i++) this.combox_city.addItem(str_ptr2[i]);
			this.step3_panel2.revalidate();
			this.step3_panel2.repaint();
		}
	}
	private void create_work_pane_step1() {
			
			JPanel work_pane_step1_panel1 = new JPanel();
			JPanel work_pane_step1_panel2 = new JPanel();
			JPanel work_pane_step1_panel3 = new JPanel();
			JPanel work_pane_step1_panel4 = new JPanel();
			
			work_pane_step1_panel1.add(this.label_telephone);
			work_pane_step1_panel1.add(this.text_telephone);
			
			work_pane_step1_panel2.add(this.label_pwd1);
			work_pane_step1_panel2.add(this.text_pwd1);
			
			work_pane_step1_panel3.add(this.label_pwd2);		
			work_pane_step1_panel3.add(this.text_pwd2,BorderLayout.SOUTH);
			
			work_pane_step1_panel4.add(this.button_step1_next);
			work_pane_step1_panel4.add(this.button_step1_exit);
			
			work_pane_step1.add(work_pane_step1_panel1);
			work_pane_step1.add(work_pane_step1_panel2);
			work_pane_step1.add(work_pane_step1_panel3);
			work_pane_step1.add(work_pane_step1_panel4);
	}
	private void create_work_pane_step2() {
		JPanel step2_panel1 = new JPanel();
		step2_panel1.add(this.label_name);
		step2_panel1.add(this.text_user_name);
		JPanel step2_panel2 = new JPanel();
		step2_panel2.add(this.label_email);
		step2_panel2.add(this.text_user_email);
		JPanel step2_panel3 = new JPanel();
		step2_panel3.add(this.label_curent_city);
		step2_panel3.add(this.text_current_city);
		JPanel step2_panel4 = new JPanel();
		ButtonGroup select_sex=new ButtonGroup();
		select_sex.add(radiobutton_user_sex_male);
		select_sex.add(radiobutton_user_sex_female);
		step2_panel4.add(this.label_sex);
		step2_panel4.add(this.radiobutton_user_sex_male);
		step2_panel4.add(this.radiobutton_user_sex_female);
		
		JPanel step2_panel5 = new JPanel();
		step2_panel5.add(this.button_step2_preview);
		step2_panel5.add(this.button_step2_next);
		step2_panel5.add(this.button_step2_exit);
		
		work_pane_step2.setLayout(new FlowLayout(FlowLayout.CENTER));
		work_pane_step2.add(step2_panel1);
		work_pane_step2.add(step2_panel2);
		work_pane_step2.add(step2_panel3);
		work_pane_step2.add(step2_panel4);
		work_pane_step2.add(new JLabel("                  "));
		work_pane_step2.add(step2_panel5);
	}
	private void create_work_pane_step3() {
		
		this.combox_province = new JComboBox(this.str_province);
		this.combox_city = new JComboBox(this.str_city_zhejiang);
		this.combox_area = new JComboBox(this.str_area_hangzhou);
		
		JPanel step3_panel1 = new JPanel();
		step3_panel1.add(this.label_address);
		step3_panel1.add(new JLabel("                                                          "));
		
		step3_panel2.add(this.label_province);
		step3_panel2.add(this.combox_province);
		step3_panel2.add(this.label_city);
		step3_panel2.add(this.combox_city);
		step3_panel2.add(this.label_area);
		step3_panel2.add(this.combox_area);
		
		JPanel step3_panel3 = new JPanel();
		step3_panel3.add(this.label_address_details);
		step3_panel3.add(this.text_address_details);
		JPanel step3_panel4 = new JPanel();
		step3_panel4.add(this.button_step3_preview);
		step3_panel4.add(this.button_finish);
		step3_panel4.add(this.button_step3_exit);
		work_pane_step3.add(step3_panel1);
		work_pane_step3.add(step3_panel2);
		work_pane_step3.add(step3_panel3);
		work_pane_step3.add(step3_panel4);
	}
	public boolean test_register_step1(String user_telephone,String pwd1,String pwd2){
		if("".equals(user_telephone) || user_telephone==null) JOptionPane.showMessageDialog(null, "telephone is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(pwd1) || pwd1==null) JOptionPane.showMessageDialog(null, "password1 is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(pwd2) || pwd2==null) JOptionPane.showMessageDialog(null, "password2 is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if(!pwd1.equals(pwd2)) JOptionPane.showMessageDialog(null, "password must be same","ERROR",JOptionPane.ERROR_MESSAGE);
		else return true;
		return false;
	}
	public boolean test_register_step2( String user_name,String user_sex, String user_email, String user_current_city){
		if("".equals(user_name) || user_name==null) JOptionPane.showMessageDialog(null, "user_name is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(user_sex) || user_sex==null) JOptionPane.showMessageDialog(null, "user_sex is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(user_email) || user_email==null) JOptionPane.showMessageDialog(null, "user_email is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(user_current_city) || user_current_city==null) JOptionPane.showMessageDialog(null, "user_current_city is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else return true;
		return false;
	}
	public boolean test_register_step3(String province,String city,String area,String address_details){
		if("".equals(province) || province==null) JOptionPane.showMessageDialog(null, "province is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(city) || city==null) JOptionPane.showMessageDialog(null, "city is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(area) || area==null) JOptionPane.showMessageDialog(null, "area is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else if("".equals(address_details) || address_details==null) JOptionPane.showMessageDialog(null, "address_details is null","ERROR",JOptionPane.ERROR_MESSAGE);
		else return true;
		return false;
	}
}
